﻿import com.kelvinluck.flickr.Flickr;
import com.kelvinluck.flickr.FlickrResponseListener;
import com.kelvinluck.flickr.Person;
import com.kelvinluck.flickr.Photo;
import com.kelvinluck.flickr.Photoset;
import com.kelvinluck.flickr.Group;
import com.kelvinluck.flickr.ResultsSet;
import com.kelvinluck.util.LogWrapper;
import mx.utils.Delegate;
import mx.transitions.Tween;
import com.mentalaxis.util.easingeffects.Expo;
import com.mentalaxis.util.easingeffects.Linear;
import com.mentalaxis.debug.TraceObject;

class com.mentalaxis.flickr.badgr.Badgr {

	var apiKey : String = "424f6c498d561642574796b9c9c381da";
	// var apiKey : String = "9b33828ec6008560269483fba4b2075f";
	// Set default user id - mentalaxis
	var userNsid : String = "75556562@N00";
	var _canvas : MovieClip;
	var badgrmode : String;
	var xOrg : Number;
	var yOrg : Number;
	var clipArray:Array;
	var numberOfPhotos : Number;
	var numberOfPhotosToView : Number;
	var gridCount : Number = 0;
	var sel:Number;
	var last:Number;
	var photoSelection:Array;
	var start : Number;
	var intv : Number;
	var tempObj : MovieClip;
	var tweenFunc : Function = Expo.easeOut;
	var alphaTween : Function = Linear.easeOut;
	var zooming : Boolean;
	var tweenTime : Number = 1.5;
	var displayTime : Number;
	var longTweenTime : Number = 120;
	var freeDepth : Number;
	var photosArray : Array;
	var masterArray : Array; // contains a backup of the PhotoArray
	var _flickr : Flickr;
	var _flickrResponseListener : FlickrResponseListener;
	var person : Person;
	var loopTime : Number;
	var xMax : Number;
	var yMax : Number;
	var canvasWidth : Number; // Full size of the badge (width)
	var canvasHeight : Number; // Full size of the badge (height)
	var thumbSize : Number = 75; // Full size of the thumbs (square)
	var squareSize : Number = 38; // Size of the shrunken thumb (square)
	var listAll : Boolean;
	var rows : Number;
	var columns : Number;
	var isTagSearch : Boolean;
	var isPhotoSet : Boolean;
	var isFavourites : Boolean;
	var isUser : Boolean;
	var isGroup : Boolean;
	var frameScalingFactors : Array;
	var frameColors : Array;
	var frameAlphas : Array;
	var drawnOnce : Boolean;
	var rollingSelection : Boolean;
	var tagSearchMode : Boolean;
	var dateMode : Boolean;
	var dateAfter : String;
	var dateBefore : String;
	var sortMode : String;

	public function Badgr(
				canvas:MovieClip,
				badgrmode:String,
				tags:String,
				flickrid:String,
				limit:String,
				timing:String,
				displayTime:String,
				rows:String,
				columns:String,
				frameScaling:String,
				frameColors:String,
				frameAlphas:String,
				rollingSelection:String,
				anytag:String,
				alltag:String,
				uploaddate:String,
				takendate:String,
				datebefore:String,
				dateafter:String,
				sortMode:String
				) {
		// initalise logging to Flash's output window
		// LogWrapper.getInstance().init();
		// LogWrapper.getInstance().addTracePublisher();

		Stage.scaleMode = "noScale";
		Stage.align = "TL";

		this.tagSearchMode = strToBool(alltag);
		this.dateMode = strToBool(uploaddate);
		this.dateAfter = dateafter;
		this.dateBefore = datebefore;
		this.sortMode = sortMode;

		// Set defaults and initialise Badgr object
		frameColors = (frameColors == undefined)? "#FFFFFF,#000000,#000000,#AAAAAA,#000000" : frameColors;
		frameAlphas = (frameAlphas == undefined)? "100,100,100,100,100" : frameAlphas;
		frameScaling = (frameScaling == undefined)? "1.05,1.0,1.08,1.04" : frameScaling;

		this.loopTime = (!isNaN(Number(timing)) || Number(timing) > 3) ? Number(timing) : 3;
		this.displayTime = (!isNaN(Number(displayTime)) || Number(displayTime) > 3) ? Number(displayTime) : 3;
		this.rows = (isNaN(Number(rows))) ? 5 : Number(rows);
		this.columns = (isNaN(Number(columns))) ? 5 : Number(columns);
		this.canvasWidth = (this.columns) * squareSize;
		this.canvasHeight = (this.rows) * squareSize;
		this.badgrmode = setBadgrType(badgrmode);
		this.frameScalingFactors = numArray(frameScaling);
		this.frameColors = hexArray(frameColors);
		this.frameAlphas = numArray(frameAlphas);
		this.userNsid = (flickrid != undefined) ? flickrid : this.userNsid;
		this.numberOfPhotosToView = this.rows * this.columns;
		this.zooming = false;
		this.clipArray = new Array();
		this.rollingSelection = strToBool(rollingSelection);
		this._canvas = canvas;
		this._flickr = Flickr.getFlickr();
		this._flickr.apiKey = apiKey;

		// Define limit...
		if (limit.toLowerCase() == "all") {
			listAll = true;
		} else if(!limit || limit == undefined || isNaN(Number(limit)) ){
			this.numberOfPhotos = numberOfPhotosToView;
		} else if(Number(limit) < numberOfPhotosToView) {
			this.numberOfPhotos = numberOfPhotosToView;
		} else {
			this.numberOfPhotos = Number(limit);
		}

		// Simple status debug...
		trace("Badgr DEBUG info");
		trace(""
			+ '\tbadgrmode: ' + badgrmode
			+ '\n\ttags: ' + tags
			+ '\n\ttag mode: ' + tagSearchMode
			+ '\n\tflickrid: ' + userNsid
			+ '\n\tlimit: ' + this.numberOfPhotos
			+ '\n\ttiming: ' + this.loopTime
			+ '\n\tdisplayTime: ' + this.displayTime
			+ '\n\trows: ' + this.rows
			+ '\n\tcolumns: ' + this.columns
			+ '\n\tframeScaling: ' + frameScaling
			+ '\n\tframeColors: ' + frameColors
			+ '\n\tframeAlphas: ' + frameAlphas
			+ '\n\tsortMode: ' + sortMode
			+ '\n\tdateMode: ' + dateMode
			+ '\n\tdateAfter: ' + dateAfter
			+ '\n\tdateBefore: ' + dateBefore
		);

		// Setup flickr response listener Delegates...
		_flickrResponseListener = new FlickrResponseListener();
		_flickrResponseListener.setSuppressOutput(false);
		_flickrResponseListener.onFavoritesGetPublicList = Delegate.create(this, onFavoritesGetPublicList);
		_flickrResponseListener.onGroupsPoolsGetPhotos = Delegate.create(this, onGroupGetPoolPhotos);
		_flickrResponseListener.onPeopleGetInfo = Delegate.create(this, onPeopleGetInfo);
		_flickrResponseListener.onPeopleGetPublicPhotos = Delegate.create(this, onPeopleGetPublicPhotos);
		_flickrResponseListener.onPhotosSearch = Delegate.create(this, onPhotosSearch);
		_flickrResponseListener.onPhotosetsGetPhotos = Delegate.create(this, onPhotosetsGetPhotos);
		_flickrResponseListener.onError = Delegate.create(this, onError);

		// Actions (selected by badgrmode)
		if (isPhotoSet) {
			trace("Photoset " + userNsid);
			this._flickr.photosetsGetPhotos(userNsid);
			return;
		}
		if(isUser) {
			if(!isPhotoSet && !isFavourites && !isTagSearch) {
				if (!listAll){
					trace("User photos " + userNsid + " [Limit " + this.numberOfPhotos + "]");
					this._flickr.peopleGetPublicPhotos(userNsid, null, this.numberOfPhotos, 1);
					return;
				} else {
					trace("All user photos " + userNsid);
					this._flickr.peopleGetInfo(userNsid);
					return;
				}
			}
			if (isFavourites) {
				trace("User favourites " + userNsid);
				this._flickr.favoritesGetPublicList(userNsid, null, this.numberOfPhotos, 1);
				return;
			}
			if (isTagSearch) {
				trace("User tag search " + userNsid + " ["+tags+"]");
				var params : Object = new Object();
				params.user_id = userNsid;
				params.tags = tags;
				params.tag_mode = (tagSearchMode)? "all" : "any";

				if(dateMode) {
					if(dateBefore != null) params.max_upload_date = dateBefore;
					if(dateAfter != null) params.min_upload_date = dateAfter;
				} else {
					if(dateBefore != null) params.max_taken_date = dateBefore;
					if(dateAfter != null) params.min_taken_date = dateAfter;
				}

				if(listAll) {
					params.per_page = 500;
				} else {
					params.per_page = numberOfPhotos;
				}
				this._flickr.photosSearch(params);
				return;
			}
		}
		if(isGroup) {
			trace("Group " + userNsid);
			this._flickr.groupsPoolsGetPhotos(userNsid, null, null, 100, 1, null);
			return;
		}
		if (isTagSearch) {
			trace("Tag search " + tags);
			var params : Object = new Object();
			params.tags = tags;
			params.per_page = numberOfPhotos;
			params.tag_mode = (tagSearchMode)? "all" : "any";
			if(sortMode != undefined) {
				params.sort = sortMode;
			}
			if(dateMode) {
				if(dateBefore != undefined) params.max_upload_date = dateBefore;
				if(dateAfter != undefined) params.min_upload_date = dateAfter;
			} else {
				if(dateBefore != undefined) params.max_taken_date = dateBefore;
				if(dateAfter != undefined) params.min_taken_date = dateAfter;
			}
			this._flickr.photosSearch(params);
			return;
		}
	}

	/**
	 Flickr response listener delegate;
	 */
	private function onFavoritesGetPublicList(user:Person, resultsSet:ResultsSet):Void {
		photosArray = resultsSet.photos;
		processPhotosArray();
	}

	/**
	 Flickr response listener delegate;
	 */
	private function onGroupGetPoolPhotos (g:Group, p:Array, resultSet:ResultsSet):Void {
		photosArray = p;
		processPhotosArray();
	}

	/**
	 Flickr response listener delegate;
	 */
	private function onPeopleGetInfo(p:Person):Void {
		this.numberOfPhotos = int(Number(p.numPhotos));
		this._flickr.peopleGetPublicPhotos(userNsid, null, this.numberOfPhotos, 1);
	}

	/**
	 Flickr response listener delegate;
	 */
	private function onPeopleGetPublicPhotos(p:Person):Void {
		photosArray = p.getPhotos();
		processPhotosArray();
	}

	/**
	 Flickr response listener delegate;
	 */
	private function onPhotosSearch(photos:Array, resultsSet:ResultsSet):Void {
		photosArray = photos;
		processPhotosArray();
	}

	/**
	 Flickr response listener delegate;
	 */
	private function onPhotosetsGetPhotos(photoset:Photoset):Void {
		photosArray = photoset.getPhotos();
		processPhotosArray();
	}

	/**
	 Process the photo array returned by Flickr
	 */
	function processPhotosArray():Void {
		trace("INFO: Badgr is processing photo list");
		// create a duplicate of the photoArray;
		masterArray = photosArray.concat();

		while (photosArray.length < numberOfPhotosToView) {
			photosArray = photosArray.concat(photosArray);
		}
		// Loop for the number of photos to view...
		for (var i:Number=0; i < numberOfPhotosToView; i++) {
			var randomPhotoIndex : Number = Math.round(Math.random()*photosArray.length-1);// Pick a random photo to add to the view set...
			var thisPhoto:Photo = Photo(photosArray.splice(randomPhotoIndex, 1)[0]);// Splice it from the photo pool
			var mc:MovieClip = _canvas.createEmptyMovieClip("photo"+i, i);// Create a MovieClip for the photo
			drawPhotoFrame(mc, thisPhoto, i);// Draw the photo frame
		}
		freeDepth = _canvas.getNextHighestDepth();// Remember the next highest depth for the _canvas clip,
	}

	private function makeZoomSelection(num:Number):Array {
		var _selection : Array = new Array();
		var copy:Array = new Array();
		// Make an array of photo indexes
		for (var i : Number = 0; i < num; i++) {
			copy.push(i);
		}
		var rnd:Number;

		// Randomize the index array
		for (i = 0; i < num; i++) {
			rnd=Math.floor(Math.random()*copy.length);
			_selection.push(copy[rnd]);
			copy.splice(rnd,1);
		}
		return _selection;
	}

	private function select() : Void{
		if(!zooming) {

			if(!(photoSelection.length > 0) || photoSelection == null){
				photoSelection = makeZoomSelection(numberOfPhotosToView);
			}
			var ind : Number = int(photoSelection.shift());
			var clip : MovieClip = clipArray[ind];
			zoomIt(clip);
		}
	}

	private function zoomIt(pic : MovieClip) : Void{
		if(!zooming){
			zooming=true;
			tempObj = pic;
			pic.swapDepths(freeDepth);
			// Stop the long _alpha tween ( started in restore() )
			// Or it may interfere when we return to start this
			// Clip animation.
			pic.myTween1.stop();
			//
			pic.myTween1 = new Tween(pic, "_alpha", alphaTween, 20, 100, tweenTime, true );
			pic.myTween2 = new Tween(pic, "_x", tweenFunc, pic.xOrg, (canvasWidth/2)-(thumbSize/2), tweenTime, true );
			pic.myTween3 = new Tween(pic, "_y", tweenFunc, pic.yOrg, (canvasHeight/2)-(thumbSize/2), tweenTime, true );
			pic.myTween4 = new Tween(pic["frame"], "_xscale", tweenFunc, 10, 100, tweenTime, true);
			pic.myTween5 = new Tween(pic["frame"], "_yscale", tweenFunc, 10, 100, tweenTime, true);
			pic.myTween6 = new Tween(pic, "_xscale", tweenFunc, 49, 100, tweenTime, true);
			pic.myTween7 = new Tween(pic, "_yscale", tweenFunc, 49, 100, tweenTime, true);
			pic.myTween8 = new Tween(pic["frame"], "_alpha", alphaTween, 0, 100, tweenTime, true);
			pic.myTween8["onMotionFinished"] = Delegate.create(this, scaleDone);
		}
	}

	private function scaleDone() : Void{
		intv = setInterval(Delegate.create(this, restore), displayTime*1000, tempObj);
		tempObj._alpha = 100;
	}

	private function restore(pic:MovieClip) : Void{
		clearInterval(intv);
		pic.time = 0;
		pic.myTween0 = new Tween(pic, "_time", 0, 100, 20, longTweenTime/2, true );
		pic.myTween1 = new Tween(pic, "_alpha", alphaTween, 100, 20, longTweenTime, true );
		pic.myTween2 = new Tween(pic, "_x", tweenFunc, (canvasWidth/2)-(thumbSize/2), pic.xOrg, tweenTime, true );
		pic.myTween3 = new Tween(pic, "_y", tweenFunc, (canvasHeight/2)-(thumbSize/2), pic.yOrg, tweenTime, true );
		pic.myTween4 = new Tween(pic["frame"], "_xscale", tweenFunc, 100, 10, tweenTime, true);
		pic.myTween5 = new Tween(pic["frame"], "_yscale", tweenFunc, 100, 10, tweenTime, true);
		pic.myTween6 = new Tween(pic, "_xscale", tweenFunc, 100, 49, tweenTime/3, true);
		pic.myTween7 = new Tween(pic, "_yscale", tweenFunc, 100, 49, tweenTime/3, true);
		pic.myTween8 = new Tween(pic["frame"], "_alpha", alphaTween, 100, 0, tweenTime, true);
		pic.myTween8["onMotionFinished"] = Delegate.create(this, scaleDownDone);
		pic.myTween0["onMotionFinished"] = Delegate.create(this, replacePhoto);
	}

	private function scaleDownDone() : Void {
		zooming=false;
	}

	private function replacePhoto(picTween:Tween) : Void {
		if(numberOfPhotosToView > numberOfPhotos);
		getNewPhoto(MovieClip(picTween.obj));
	}

	private function getNewPhoto(mc:MovieClip):Void {
		if(rollingSelection) {
			if(photosArray.length > 0) {
				var randomPhotoIndex : Number = Math.round(Math.random()*photosArray.length-1); // Pick a random photo to add to the view set...
				var thisPhoto:Photo = Photo(photosArray.splice(randomPhotoIndex, 1)[0]); // Splice it from the photo pool
				mc["photo"] = thisPhoto;
				// removeMovieClip(mc["picture"]);
				var picture : MovieClip = mc.createEmptyMovieClip("picture", mc.getNextHighestDepth()); // Load photo from thumbNail URL (Flashr calls the Square image the thumbnail image)
				// Load photo from thumbNail URL (Flashr calls the Square image the thumbnail image)
				var pictureListener : Object = new Object();
				pictureListener.onLoadInit = Delegate.create(this, pictureLoaded);
				var pictureLoader:MovieClipLoader = new MovieClipLoader();
				pictureLoader.addListener(pictureListener);
				pictureLoader.loadClip(thisPhoto.thumbnailUrl, picture);
			} else {
				photosArray = masterArray.concat();
				if(photosArray.length > 0) getNewPhoto(mc);
			}
		}
	}

	private function drawRect(mc:MovieClip, color:Number, alpha:Number, a:Number, b:Number, c:Number, d:Number) : Void {
		mc.beginFill(color, alpha);
		mc.moveTo(b, d); mc.lineTo(a, d);
		mc.lineTo(a, c); mc.lineTo(b, c);
		mc.lineTo(b, d); mc.endFill();
	}

	function getGridPos(i:Number, columns:Number, cellsize:Number):Object {
		var xpos : Number = (i-((columns)*Math.floor(i/columns))) * cellsize;
		var ypos : Number = Math.floor(i / columns) * cellsize;
		return {x:xpos, y:ypos};
	}

	private function drawPhotoFrame(mc:MovieClip, thisPhoto:Photo, i:Number) : Void {
		var debugString : String = "";
		// Place photo frame clip at relative position to i (index).
		mc._x = getGridPos(i, columns, squareSize).x;
		mc._y = getGridPos(i, columns, squareSize).y;

		// Create visual frame
		var frame : MovieClip = mc.createEmptyMovieClip("frame", 1);
		var a:Number = (thumbSize/2);
		var b:Number, c:Number, d:Number;

		// Offset photo frame for scaling.
		frame._x = a;
		frame._y = a;
		frame._alpha = 0;

		// Expand the frame to the size of the canvas
		a = canvasWidth/2;
		b = -(canvasWidth/2);
		c = canvasHeight/2;
		d = -(canvasHeight/2);

		// Draw outer border
		drawRect(frame, frameColors[0], frameAlphas[0], a, b, c, d);

		a = a/frameScalingFactors[0];
		b = b/frameScalingFactors[0];
		c = c/frameScalingFactors[0];
		d = d/frameScalingFactors[0];

		// Draw outer border
		drawRect(frame, frameColors[1], frameAlphas[1], a, b, c, d);

		a = a/frameScalingFactors[1];
		b = b/frameScalingFactors[1];
		c = c/frameScalingFactors[1];
		d = d/frameScalingFactors[1];

		// Draw main frame
		drawRect(frame, frameColors[2], frameAlphas[2], a, b, c, d);

		// Draw the frame around the thumb.

		a = (thumbSize/2);
		b = -(thumbSize/2);
		c = (thumbSize/2);
		d = -(thumbSize/2);

		a = a*frameScalingFactors[2];
		b = b*frameScalingFactors[2];
		c = c*frameScalingFactors[2];
		d = d*frameScalingFactors[2];


		// Draw keyline
		drawRect(frame, frameColors[3], frameAlphas[3], a, b, c, d);

		a = a/frameScalingFactors[3];
		b = b/frameScalingFactors[3];
		c = c/frameScalingFactors[3];
		d = d/frameScalingFactors[3];

		// Draw inner border
		drawRect(frame, frameColors[4], frameAlphas[4], a, b, c, d);

		// Create jpg holder
		var picture : MovieClip = mc.createEmptyMovieClip("picture", 2);

		// Load photo from thumbNail URL (Flashr calls the Square image the thumbnail image)
		var pictureListener : Object = new Object();
		pictureListener.onLoadInit = Delegate.create(this, pictureLoaded);
		var pictureLoader:MovieClipLoader = new MovieClipLoader();
		pictureLoader.addListener(pictureListener);
		pictureLoader.loadClip(thisPhoto.thumbnailUrl, picture);
		// picture.loadMovie(thisPhoto.thumbnailUrl);

		// Scale the 75px square to fit 38px;
		mc._xscale=mc._yscale=49;

		// Fade the photo back to 20%
		mc._alpha = 0;

		// Assign the photo to the clip photo property
		mc.photo = thisPhoto;

		// Set origins
		mc.xOrg = mc._x;
		mc.yOrg = mc._y;

		var pictureButton : MovieClip = mc.createEmptyMovieClip("pictureButton", 10);
		pictureButton.beginFill(0, 0);
		pictureButton.moveTo(0,0);
		pictureButton.lineTo(thumbSize, 0);
		pictureButton.lineTo(thumbSize, thumbSize);
		pictureButton.lineTo(0, thumbSize);
		pictureButton.lineTo(0, 0);
		pictureButton.endFill();
		pictureButton.photo = mc["photo"];

		// Set on press for the photo to open the Flickr photo page (as per Flickr.com TOS)
		pictureButton.onRelease = function(){
			getURL(this["photo"].photoPageUrl);
		};

		// Push the photo clip into the clip array.
		clipArray.push(mc);

	}

	private function pictureLoaded(target:MovieClip):Void {
		var t:Tween = new Tween (target._parent, "_alpha", tweenFunc, 0, 20, 1, true);
		t["onMotionFinished"] = Delegate.create(this, pictureTweened);
	}

	private function pictureTweened():Void {
		gridCount ++;
		if(gridCount == numberOfPhotosToView) {
			// All photos are loaded
			// Start the select loop
			this.start = setInterval(Delegate.create(this, select), loopTime*1000);
		}
	}

	/**
	 * Converts a String to a Boolean based on the commonly
	 * used string indicators of true or false.
	 */
	private function strToBool(inStr:String):Boolean {
		switch(inStr.toLowerCase()) {
			case "true" : return true;
			case "false" : return false;
			case "yes" : return true;
			case "no" : return false;
			case "1" : return true;
			case "0" : return false;
			case "y" : return true;
			case "n" : return false;
			// if the value isn't recognised return false;
			default : return false;
		}
	}

	/**
	 * Converts a string array (comma separated) of hex colours to
	 * an Array or numeric color values
	 *
	 * @param   inStr - Comma separated string of hex color values.
	 *
	 * @return  an Array of Color values from the string.
	 */
	private function hexArray(inStr:String):Array {
		var tempArray : Array = inStr.split(",");
		var tempString : String;
		for(var a : Number = 0; a < tempArray.length; a++) {
			tempArray[a] = Number("0x" + strip("#", tempArray[a]));
		}
		return tempArray;
	}

	/**
	 * Convert a string containing a comma separated collection
	 * of numbers to an array of Number values
	 *
	 * @param   inStr - Comma separated string of numbers
	 * @return  an Array of the Number values contained in inStr
	 */
	private function numArray (inStr:String):Array {
		var tempArray : Array = inStr.split(",");
		var tempString : String;
		for(var a : Number = 0; a < tempArray.length; a++) {
			tempArray[a] = Number(tempArray[a]);
		}
		return tempArray;
	}

	/**
	 * Strip a string of chars from another
	 * string with a split/join operation.
	 *
	 * @param   stripChar - String to find and remove
	 * @param   inStr - String to strip from
	 * @return a inStr String stripChar instances removed.
	 */
	private function strip(stripChar:String, inStr:String):String {
		return inStr.split(stripChar).join("");
	}

	/**
	 * Set the badgr mode based on the incoming string from the URL
	 * &badgrmode=
	 *
	 * @param   _mode - one of the following modes.
	 *
	 * 1 = user - User photos are returned
	 * 2 = user set - A Photoset is returned
	 * 3 = user favourites - Users favourites are returned
	 * 4 = user tag search - User / Tag search results returned
	 * 5 = group - Group / Pool photos are returned
	 * 6 = all tag search - Global Tag search results are returned
	 *
	 * @return the mode as a String for debug pruposes, for example if
	 * the mode is left null return the default mode (1)
	 */
	private function setBadgrType(_mode:String):String {

		isTagSearch = false;
		isGroup = false;
		isUser = false;
		isFavourites = false;
		isPhotoSet = false;

		if(Number(_mode) > 6 || _mode == undefined || Number(_mode) < 1) _mode = "1";

		switch(Number(_mode)) {
			case 1: {
				isUser = true;
				break;
			}
			case 2: {
				isPhotoSet = true;
				break;
			}
			case 3: {
				isUser = true;
				isFavourites = true;
				break;
			}
			case 4: {
				isUser = true;
				isTagSearch = true;
				break;
			}
			case 5: {
				isGroup = true;
				break;
			}
			case 6: {
				isTagSearch = true;
				break;
			}
		}
		return _mode.toString();
	}

	/**
	 * Display a message to output if the Flickr API call fails.
	 * Used as a Delegate of _flickrResponseListener.onError
	 */
	private function onError(error:Number, message:String, method:String):Void {
		trace(error + ": " + message + " - " + method);
	}

	/**
	 * toString method for object identification.
	 */
	public function toString():String {
		return "[com.mentalaxis.flickr.badgr.Badgr]";
	}
}